package com.laikan.reader.core.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.laikan.reader.core.LaikanBaseAdapter;
import com.laikan.reader.R;
import com.laikan.reader.core.bean.FeedBackBean;
import com.laikan.reader.core.utils.PublicUtil;

import java.util.ArrayList;

public class FeedBackAdapter extends LaikanBaseAdapter<FeedBackBean> {

    public FeedBackAdapter(Context context, ArrayList<FeedBackBean> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.lk_feedback_item, null);
            holder.left_content = (TextView)convertView.findViewById(R.id.left_content);
            holder.right_content = (TextView)convertView.findViewById(R.id.right_content);
            holder.lin_left = (LinearLayout)convertView.findViewById(R.id.lin_left);
            holder.lin_right = (LinearLayout)convertView.findViewById(R.id.lin_right);
            holder.feedback_time = (TextView)convertView.findViewById(R.id.feedback_time);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }

        // TODO 设置时间显示规则

        if (position == 0) {
            holder.feedback_time.setVisibility(View.VISIBLE);
        }
        try {
            if ((Long.parseLong(datas.get(position).getCreateTime()) - Long.parseLong(datas.get(position - 1).getCreateTime())) > 600000) {
                holder.feedback_time.setVisibility(View.VISIBLE);
            }else {
                if (position == 0) {
                    holder.feedback_time.setVisibility(View.VISIBLE);
                }else {
                    holder.feedback_time.setVisibility(View.GONE);
                }
            }
        }catch (Exception e) {
        }
        if ("3".equals(datas.get(position).getType())) {
            holder.feedback_time.setText("------ " + PublicUtil.getDateToString(Long.parseLong(datas.get(position).getCreateTime())) + " -----");
            // 用户反馈
            holder.lin_left.setVisibility(View.GONE);
            holder.lin_right.setVisibility(View.VISIBLE);
            holder.right_content.setText(datas.get(position).getContent());
        }else if ("2".equals(datas.get(position).getType())) {
            // 服务器反馈
            holder.lin_right.setVisibility(View.GONE);
            holder.lin_left.setVisibility(View.VISIBLE);
            holder.left_content.setText(datas.get(position).getContent());
        }

        return convertView;
    }

    private class ViewHolder {

        private LinearLayout lin_left, lin_right;
        private TextView right_content;
        private TextView left_content;
        private TextView feedback_time;
    }
}
